<?php

require_once("../../../../wp-blog-header.php");

//global $wp_query;
//global $wp,$wpdb;
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$month = array("",
    "Enero",
    "Febrero",
    "Marzo",
    "Abril",
    "Mayo",
    "Junio",
    "Julio",
    "Agosto",
    "Setiembre",
    "Octubre",
    "Noviembre",
    "Diciembre"
    );

$consumption_water_registry = $wpdb->get_results($wpdb->prepare("select * from bpa_registry_consumption where user_id = %d and type_id = 1 order by year asc, month asc", $user_id));
$consumption_electricity_registry = $wpdb->get_results($wpdb->prepare("select * from bpa_registry_consumption where user_id = %d and type_id = 2 order by year asc, month asc", $user_id));
$consumption_fuel_registry = $wpdb->get_results($wpdb->prepare(
        "select c.user_id, c.year, c.month, c.quantity, c.price, st.name 
        from bpa_registry_consumption c
        left join bpa_consumption_subtype st
        on c.subtype_id = st.id
        where c.user_id = %d and c.type_id = 3
        order by c.year asc, c.month asc, c.subtype_id asc", 
        $user_id));


/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once dirname(__FILE__) . '/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Ecopyme")
							 ->setLastModifiedBy("Ecopyme")
							 ->setTitle("Office 2007 XLSX Data")
							 ->setSubject("Office 2007 XLSX Data")
							 ->setDescription("Office 2007 XLSX Data")
							 ->setKeywords("office 2007 openxml php")
							 ->setCategory("Ecopyme");


// Add water data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Año')
            ->setCellValue('B1', 'Mes')
            ->setCellValue('C1', 'Consumo m3')
            ->setCellValue('D1', 'Soles');

$row = 2;
foreach ( $consumption_water_registry as $item ) {
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$row, $item->year)
            ->setCellValue('B'.$row, $month[$item->month])
            ->setCellValue('C'.$row, $item->quantity)
            ->setCellValue('D'.$row, $item->price);
    $row++;
} 

$objPHPExcel->getActiveSheet()->setTitle('Agua');


// Add electricity data
$objPHPExcel->createSheet(NULL, 1);
$objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A1', 'Año')
            ->setCellValue('B1', 'Mes')
            ->setCellValue('C1', 'Consumo kWh')
            ->setCellValue('D1', 'Soles');

$row = 2;
foreach ( $consumption_electricity_registry as $item ) {
    $objPHPExcel->setActiveSheetIndex(1)
            ->setCellValue('A'.$row, $item->year)
            ->setCellValue('B'.$row, $month[$item->month])
            ->setCellValue('C'.$row, $item->quantity)
            ->setCellValue('D'.$row, $item->price);
    $row++;
} 

$objPHPExcel->getActiveSheet()->setTitle('Electricidad');


// Add fuel data
$objPHPExcel->createSheet(NULL, 2);
$objPHPExcel->setActiveSheetIndex(2)
            ->setCellValue('A1', 'Año')
            ->setCellValue('B1', 'Mes')
            ->setCellValue('C1', 'Tipo')
            ->setCellValue('D1', 'Consumo m3')
            ->setCellValue('E1', 'Soles');

$row = 2;
foreach ( $consumption_fuel_registry as $item ) {
    $objPHPExcel->setActiveSheetIndex(2)
            ->setCellValue('A'.$row, $item->year)
            ->setCellValue('B'.$row, $month[$item->month])
            ->setCellValue('C'.$row, $item->name)
            ->setCellValue('D'.$row, $item->quantity)
            ->setCellValue('E'.$row, $item->price);
    $row++;
} 

$objPHPExcel->getActiveSheet()->setTitle('Combustible');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="export.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
